# Script by Danke!

# name			- 1 - carrying intel
# 			- 2 - after capture / carrying second intel 
# 			- 3 - after capturing again

#  armor		- 1 - all non-headshot, non-melee hits against you do 1/4 damage
#			- 2 - all non-headshot, non-melee hits against you do 1/8 damage
#			- 3 - all non-headshot, non-melee hits against you do no damage

#  deadly dice		- 1 - 10% enemies you hit die instantly
#			- 2 - 20% enemies you hit die instantly
#			- 3 - 30% enemies you hit die instantly

#  Good Teammate	- 1 - your entire team is healed 20 points every time you get a headshot
#			- 2 - your entire team is healed 30 points every time you kill
#			- 3 - 50 headshot / 30 regularly

#  teleportation	- 1 - you are safely teleported to where your nade goes off
#			- 2 - you are teleported to where you break a block with your gun
#			- 3 - you are healed when you teleport

#  regenerate		- 1 - you are healed 2hp every two seconds
#			- 2 - you are healed 5hp every two seconds
#			- 3 - you are healed 10hp every two seconds

#  poison bullets	- 1 - enemies you hit take 1 damage/sec until they touch the tent or die
#			- 2 - enemies you hit take 2 damage/sec until they touch the tent or die
#			- 3 - enemies you hit take 3 damage/sec until they touch the tent or die

from pyspades.server import block_action
from pyspades.constants import *
from commands import add
import random
import commands

ARMOR, DEADLY_DICE, TEAMMATE, TELEP, REGEN, POISON =  xrange(6)
def power(connection, value = 6):
    value = int(value)
    if value > 5:
        connection.send_chat("Type /powerpref for info on setting prefered power")
        connection.send_chat('4 - Regeneration      5 - Poison Bullets')
        connection.send_chat('2 - Good Teammate     3 - Teleportation')
        connection.send_chat('0 - Armor             1 - Deadly Dice')
        connection.send_chat("Type /power # to read about a power:")
    elif value == ARMOR:
        connection.send_chat("Take less damage from body-shots and grenades.")
        connection.send_chat("The power of Armor:")
    elif value == DEADLY_DICE:
        connection.send_chat("A chance to instantly kill any target you hit.")
        connection.send_chat("The power of Deadly Dice:")
    elif value == TEAMMATE:
        connection.send_chat("Every kill you make will heal you and your teammates.")
        connection.send_chat("The power of the Good Teammate:")
    elif value == TELEP:
        connection.send_chat("Use grenades or your weapon to teleport distances.")
        connection.send_chat("The power of Teleportation:")
    elif value == REGEN:
        connection.send_chat("Regenerate HP slowly over time.")
        connection.send_chat("The power of Regeneration:")
    elif value == POISON:
        connection.send_chat("Your bullets continue to hurt enemies long after they've hit")
        connection.send_chat("The power of Poison Bullets:")
add(power)

def powerpref(connection, value = 6):
    value = int(value)
    if value > 5:
        connection.send_chat('4 - Regeneration      5 - Poison Bullets')
        connection.send_chat('2 - Good Teammate     3 - Teleportation')
        connection.send_chat('0 - Armor             1 - Deadly Dice')
        connection.send_chat("Type /powerpref # to set a preference")
        connection.send_chat("Preference is chosen on the FOURTH capture.")
    elif value <= 5:
        connection.intel_power_pref = value
        connection.send_chat("Preference saved.")
add(powerpref)


def apply_script(protocol, connection, config):
    class IntelPowerConnection(connection):
        poisoner = None
        poison = 0
        intel_power_level = 0
        intel_power = None
        intel_power_name = None
        intel_power_pref = 6

        def explain_power(self):
            if self.intel_power == ARMOR:
                self.send_chat("You take less damage from body-shots and grenades.")
                self.send_chat("You have the power of Armor, level %s" % self.intel_power_level)
            elif self.intel_power == DEADLY_DICE:
                self.send_chat("You have a chance to instantly kill any target you hit.")
                self.send_chat("You have the power of Deadly Dice, level %s" % self.intel_power_level)
            elif self.intel_power == TEAMMATE:
                if self.intel_power_level == 1:
                    self.send_chat("Every headshot you make will heal you and your teammates.")
                if self.intel_power_level == 2:
                    self.send_chat("Every kill you make will heal you and your teammates.")
                if self.intel_power_level == 3:
                    self.send_chat("Every kill you make will heal you and your teammates. Headshots heal more.")
                self.send_chat("You have the power of the Good Teammate, level %s" % self.intel_power_level)
            elif self.intel_power == TELEP:
                if self.intel_power_level == 1:
                    self.send_chat("Throw grenades to teleport where they land!")
                if self.intel_power_level == 2:
                    self.send_chat("Break blocks with bullets to teleport where they were!")
                if self.intel_power_level == 3:
                    self.send_chat("Break blocks with bullets to teleport where they were and refill your health!")
                self.send_chat("You have the power of Teleportation, level %s" % self.intel_power_level)
            elif self.intel_power == REGEN:
                self.send_chat("You regenerate HP slowly over time.")
                self.send_chat("You have the power of Regeneration, level %s" % self.intel_power_level)
            elif self.intel_power == POISON:
                self.send_chat("Your bullets continue to hurt enemies long after they've hit")
                self.send_chat("You have the power of Poison Bullets, level %s" % self.intel_power_level)

        def on_hit(self, hit_amount, hit_player, type):
            value = connection.on_hit(self, hit_amount, hit_player, type)
            if value is not None or self.team == hit_player.team:
                return value
            value = hit_amount
            if self.intel_power == DEADLY_DICE:
                dice_roll = random.randint(1, 100)
                if dice_roll <= 30 and self.intel_power_level == 3:
                    value = 100
                    hit_player.send_chat("You have been instakilled by %s !" % self.name)
                if dice_roll <= 20 and self.intel_power_level == 2:
                    value = 100                      
                    hit_player.send_chat("You have been instakilled by %s !" % self.name)
                if dice_roll <= 10 and self.intel_power_level == 1:
                    value = 100
                    hit_player.send_chat("You have been instakilled by %s !" % self.name)
            if self.intel_power == POISON:
                hit_player.send_chat("You have been poisoned by %s ! Get to the tent to cure it!" % self.name)
                hit_player.poisoner = self
                hit_player.poison = self.intel_power_level
            if hit_player.intel_power == ARMOR:
                if type != HEADSHOT_KILL and type != MELEE_KILL:
                    if hit_player.intel_power_level == 3:
                        return 0
                    if hit_player.intel_power_level == 2:
                        value *= .125
                    if hit_player.intel_power_level == 1:
                        value *= .25
                else:
                    self.send_chat("%s is wearing armor! Aim for the head!" % hit_player.name )
            return value

        def on_kill(self, killer, type):
            if killer != self and killer is not None:
                if killer.intel_power is not None:
                    self.send_chat("Type /power for more info on Intel Powers")
                    self.send_chat("You were killed by %s who had Intel Power: %s level %s" % (killer.name, killer.intel_power_name, killer.intel_power_level) )
                if killer.intel_power == TEAMMATE:
                    killer.send_chat("Your kill has healed you and your teammates!")
                    if killer.intel_power_level != 2 and type == HEADSHOT_KILL:
                        killer.heal_team(20)
                    if killer.intel_power_level > 1:
                        killer.heal_team(30)
            self.poisoner = None
            self.poison = 0
            self.intel_power_level = 0
            self.intel_power = None
            self.intel_power_name = None
            return connection.on_kill(self, killer, type)
        
        def heal_team(self, value):
            for player in self.team.get_players():
                player.set_hp(player.hp + value, type = FALL_KILL)
        def on_position_update(self):
            if self.poison > 0:
                self.hit(self.poison, self.poisoner)
            if self.intel_power == REGEN:
                if self.intel_power_level == 3:
                    self.set_hp(self.hp + 10, type = FALL_KILL)
                if self.intel_power_level == 2:
                    self.set_hp(self.hp + 5, type = FALL_KILL)
                if self.intel_power_level == 1:
                    self.set_hp(self.hp + 2, type = FALL_KILL)
            connection.on_position_update(self)

        def on_refill(self):
            if self.poison > 0:
                self.send_chat("You have been cured of the poison!")
                self.poison = 0
                self.poisoner = None
            return connection.on_refill(self)
        
        def on_flag_take(self):
            if connection.on_flag_take(self) is not False:
                if self.intel_power_level == 0:
                    self.intel_power_level = 1
                    self.intel_power = self.protocol.current_power
                    self.intel_power_name = self.protocol.current_power_name
                    self.explain_power()

        def on_flag_capture(self):
            if connection.on_flag_capture(self) is not False:
                self.protocol.set_current_power()
                if self.intel_power_level == 3:
                    if self.intel_power_pref <= 5:
                        self.intel_power = self.intel_power_pref
                    else:
                        self.send_chat("Use /powerpref to set your power preference!")
                if self.intel_power_level < 3:
                    self.intel_power_level += 1
                self.explain_power()
            connection.on_flag_capture(self)

        def on_block_destroy(self, x, y, z, mode):
            if self.intel_power_level >= 2 and self.intel_power == TELEP and mode == DESTROY_BLOCK and self.tool == WEAPON_TOOL:
                self.clear_tele_area(x, y, z)
                self.set_location_safe((x, y, z-1))
                if self.intel_power_level == 3:
                    self.set_hp(100, type = FALL_KILL)
            if self.intel_power_level == 1 and self.intel_power == TELEP and mode == GRENADE_DESTROY:
                self.set_location_safe((x, y, z-1))
            return connection.on_block_destroy(self, x, y, z, mode)

        def clear_tele_area(self, x, y, z):
            map = self.protocol.map
            for nade_x in xrange(x - 1, x + 2):
                for nade_y in xrange(y - 1, y + 2):
                    for nade_z in xrange(z - 1, z + 2):
                        if nade_x > 0 and nade_x < 512 and nade_y > 0 and nade_y < 512 and nade_z > 0 and nade_z < 63:
                            map.remove_point(nade_x, nade_y, nade_z)
            block_action.x = x
            block_action.y = y
            block_action.z = z
            block_action.value = 3
            block_action.player_id = self.player_id
            self.protocol.send_contained(block_action, save = True)
    class IntelPowerProtocol(protocol):
        current_power = 4
        current_power_name = "Teleportation"

        def set_current_power(self):
            dice_roll = random.randint(1, 6)
            if dice_roll == 1:
                self.current_power = ARMOR
                self.current_power_name = "Armor"
            if dice_roll == 2:
                self.current_power = DEADLY_DICE
                self.current_power_name = "Deadly Dice"
            if dice_roll == 3:
                self.current_power = TEAMMATE
                self.current_power_name = "Good Teammate"
            if dice_roll == 4:
                self.current_power = TELEP
                self.current_power_name = "Teleportation"
            if dice_roll == 5:
                self.current_power = REGEN
                self.current_power_name = "Regeneration"
            if dice_roll == 6:
                self.current_power = POISON
                self.current_power_name = "Poison Bullets"

    return IntelPowerProtocol, IntelPowerConnection
 

